#include "stdafx.h"
#include "SampleAppForWM6.h"
#include <windows.h>
#include <commctrl.h>

#define MAX_LOADSTRING 100

HINSTANCE			g_hInst;
HWND				g_hWndMenuBar;

//////////////////////////////////////////////////////////////////////////
// Start of TRUEMapLib
//////////////////////////////////////////////////////////////////////////
#define ID_BUTTON_BASE							0x1234
#define ID_BUTTON_ZOOM_IN						(ID_BUTTON_BASE + 0)
#define ID_BUTTON_ZOOM_OUT						(ID_BUTTON_BASE + 1)
#define ID_BUTTON_GOTO_TRUEMOBILE				(ID_BUTTON_BASE + 2)

TMMapHandle			g_hTMMap;
HWND				g_hButtonZoomIn;
HWND				g_hButtonZoomOut;
HWND				g_hButtonGoToTRUEMobile;
//////////////////////////////////////////////////////////////////////////
// End of TRUEMapLib
//////////////////////////////////////////////////////////////////////////

ATOM				MyRegisterClass(HINSTANCE, LPTSTR);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	MSG msg;

	if (!InitInstance(hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	HACCEL hAccelTable;
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SAMPLEAPPFORWM6));

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SAMPLEAPPFORWM6));
	wc.hCursor       = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = szWindowClass;

	return RegisterClass(&wc);
}

//////////////////////////////////////////////////////////////////////////
// Start of TRUEMapLib
//////////////////////////////////////////////////////////////////////////
TMVoid __stdcall OnMapEvent(TMMapHandle hMap, TMEventType eventType, LPARAM lParam, WPARAM wParam, TMVoid *pUserParam)
{
	int nUserParam;

	// The pUserParam argument is 3rd argument of tmRegisterEventCallback() in InitInstance().
	// In this sample, the argument value is like this:
	// tmRegisterEventCallback(g_hTMMap, OnMapEvent, (TMVoid *)10);
	// So pUserParam is to be '(TMVoid *)10'.
	nUserParam = (int)pUserParam;

	// You may use nUserParam in here.

	// Print eventType to output window to test event callback.
	switch(eventType)
	{
	case TMEventTypeMapWillLoad:
		OutputDebugString(_T("TMEventTypeMapWillLoad\n"));
		break;

	case TMEventTypeMapLoaded:
		OutputDebugString(_T("TMEventTypeMapLoaded\n"));
		break;

	case TMEventTypeMapWillDraw:
		OutputDebugString(_T("TMEventTypeMapWillDraw\n"));
		break;

	case TMEventTypeMapDrawn:
		OutputDebugString(_T("TMEventTypeMapDrawn\n"));
		break;
	}
}
//////////////////////////////////////////////////////////////////////////
// End of TRUEMapLib
//////////////////////////////////////////////////////////////////////////

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
    TCHAR szTitle[MAX_LOADSTRING];
    TCHAR szWindowClass[MAX_LOADSTRING];

    g_hInst = hInstance;

	SHInitExtraControls();

    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_SAMPLEAPPFORWM6, szWindowClass, MAX_LOADSTRING);

    hWnd = FindWindow(szWindowClass, szTitle);	
    if (hWnd) 
    {
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        return 0;
    } 

    if (!MyRegisterClass(hInstance, szWindowClass))
    {
    	return FALSE;
    }

    hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

	//////////////////////////////////////////////////////////////////////////
	// Start of TRUEMapLib
	//////////////////////////////////////////////////////////////////////////
	RECT rcWindow;
	RECT rcTMMap;
	int nWindowWidth;
	int nWindowHeight;
	TMLocation location;

	// start location
	location.dLatitude = 37.433446;
	location.dLongitude = 127.173493;

	GetClientRect(hWnd, &rcWindow);

	nWindowWidth = rcWindow.right - rcWindow.left;
	nWindowHeight = rcWindow.bottom - rcWindow.top;

	SetRect(&rcTMMap, 0, 0, nWindowWidth, nWindowHeight - 70);

	// Initialize TRUE.MapLib
	if(tmInitLibrary() == TRUE)
	{
		// It creates the TRUEMapLib object.
		g_hTMMap = tmCreate(WS_CHILD | WS_BORDER | WS_VISIBLE, rcTMMap, hWnd, hInstance);

		// It registers callback function to get map events.
		tmRegisterEventCallback(g_hTMMap, OnMapEvent, (TMVoid *)10);

		if(g_hTMMap != NULL)
		{
			// It loads and shows the map from Google(TM) Maps server.
			tmLoad(g_hTMMap, TMMapTypeGoogle, location, 17);
		}
		else
		{
			// An error occurred while creating TRUEMapLib object.
			MessageBox(hWnd, _T("Could not create TRUEMapLib object"), _T("TRUE.Map"), MB_OK);
		}

		// Create buttons.
		g_hButtonZoomIn = CreateWindow(_T("button"), _T("Zoom In"), WS_CHILD | WS_VISIBLE, 0, rcTMMap.bottom, nWindowWidth / 3, 30, hWnd, (HMENU)ID_BUTTON_ZOOM_IN, hInstance, NULL);
		g_hButtonZoomOut = CreateWindow(_T("button"), _T("Zoom Out"), WS_CHILD | WS_VISIBLE, nWindowWidth / 3, rcTMMap.bottom, nWindowWidth / 3, 30, hWnd, (HMENU)ID_BUTTON_ZOOM_OUT, hInstance, NULL);
		g_hButtonGoToTRUEMobile = CreateWindow(_T("button"), _T("TRUEMobile"), WS_CHILD | WS_VISIBLE, nWindowWidth / 3 * 2, rcTMMap.bottom, nWindowWidth / 3, 30, hWnd, (HMENU)ID_BUTTON_GOTO_TRUEMOBILE, hInstance, NULL);
	}
	else
	{
		// An error occurred while initializing TRUEMapLib.
		MessageBox(hWnd, _T("Could not initialize TRUEMapLib"), _T("TRUE.Map"), MB_OK);
	}
	//////////////////////////////////////////////////////////////////////////
	// End of TRUEMapLib
	//////////////////////////////////////////////////////////////////////////

	if (g_hWndMenuBar)
    {
        RECT rc;
        RECT rcMenuBar;

        GetWindowRect(hWnd, &rc);
        GetWindowRect(g_hWndMenuBar, &rcMenuBar);
        rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
		
        MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    static SHACTIVATEINFO s_sai;
	
    switch (message) 
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam); 
            wmEvent = HIWORD(wParam); 
            switch (wmId)
            {
                case IDM_HELP_ABOUT:
                    DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
                    break;
                case IDM_OK:
                    SendMessage (hWnd, WM_CLOSE, 0, 0);				
                    break;

				//////////////////////////////////////////////////////////////////////////
				// Start of TRUEMapLib
				//////////////////////////////////////////////////////////////////////////
				case ID_BUTTON_ZOOM_IN:
					// If the zoom-in button is pressed.
					tmZoomIn(g_hTMMap);
					break;

				case ID_BUTTON_ZOOM_OUT:
					// If the zoom-out button is pressed.
					tmZoomOut(g_hTMMap);
					break;

				case ID_BUTTON_GOTO_TRUEMOBILE:
					{
						TMLocation location;

						// Set the default location
						location.dLatitude = 37.433446;
						location.dLongitude = 127.173493;

						// Calls the tmSetMapCenterLocation() API to change latitude/longitude
						tmSetMapCenterLocation(g_hTMMap, location);

						// Calls the tmSetZoomLevel() API to change zoom level
						tmSetZoomLevel(g_hTMMap, 6);
					}

					break;
				//////////////////////////////////////////////////////////////////////////
				// End of TRUEMapLib
				//////////////////////////////////////////////////////////////////////////

                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
        case WM_CREATE:
            SHMENUBARINFO mbi;

            memset(&mbi, 0, sizeof(SHMENUBARINFO));
            mbi.cbSize     = sizeof(SHMENUBARINFO);
            mbi.hwndParent = hWnd;
            mbi.nToolBarId = IDR_MENU;
            mbi.hInstRes   = g_hInst;

            if (!SHCreateMenuBar(&mbi)) 
            {
                g_hWndMenuBar = NULL;
            }
            else
            {
                g_hWndMenuBar = mbi.hwndMB;
            }

            memset(&s_sai, 0, sizeof (s_sai));
            s_sai.cbSize = sizeof (s_sai);
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
			//////////////////////////////////////////////////////////////////////////
			// Start of TRUEMapLib
			//////////////////////////////////////////////////////////////////////////
			// Destroy map control.
			tmDestroy(g_hTMMap);

			// Uninitialize TRUE.MapLib
			tmUninitLibrary();

			// Destroy two buttons.
			DestroyWindow(g_hButtonZoomIn);
			DestroyWindow(g_hButtonZoomOut);
			//////////////////////////////////////////////////////////////////////////
			// End of TRUEMapLib
			//////////////////////////////////////////////////////////////////////////

            CommandBar_Destroy(g_hWndMenuBar);
            PostQuitMessage(0);
            break;

        case WM_ACTIVATE:
            SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
            break;
        case WM_SETTINGCHANGE:
            SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                SHINITDLGINFO shidi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);
            }
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return TRUE;

    }
    return (INT_PTR)FALSE;
}
